<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="备件名称" prop="title">
                        <el-input v-model="form.title" placeholder="请输入备件名称" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="备件编号" prop="code">
                        <el-input v-model="form.code" placeholder="备件编号" clearable autocomplete="off"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="类别" prop="class_id">
                        <el-select v-model="form.class_id"  filterable style="width: 100%" placeholder="请选择备件类别" clearable>
                            <el-option v-for="item in fitList" :key="item.id" :label="item.title" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="备件规格" prop="spec">
                        <el-input v-model="form.spec" placeholder="备件规格" clearable autocomplete="off"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="单位" prop="unit_id">
                        <el-select v-model="form.unit_id"  filterable style="width: 100%" placeholder="请选择备件类别" clearable>
                            <el-option v-for="item in unitList" :key="item.id" :label="item.title" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="form.remark" placeholder="请输入备注" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="排序" prop="sort">
                        <el-input v-model="form.sort" placeholder="请输入排序" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="是否禁用" prop="system_disable">
                        <el-switch v-model="form.system_disable" :active-value="1" :inactive-value="0"></el-switch>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="图片" prop="img">
                <sc-upload v-model="form.img" draggable></sc-upload>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增备件',
				edit: '编辑备件',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: "",
				title: "",
				code: "",
				sort: "",
				remark: "",
                class_id:"",
                spec:"",
                unit_id:"",
                img:"",
                system_disable: 0,
			},
			//验证规则
			rules: {
				title: [
					{ required: true, message: '请输入备件名称' }
				],
				code: [
					{ required: true, message: '请输入备件编号' }
				],
                class_id: [
					{ required: true, message: '请选择类别' }
				],
                unit_id: [
					{ required: true, message: '请选择单位' }
				],
			},
            unitList:[],
            fitList:[]
		}
	},
	mounted() {
        this.getUnit()
        this.getFittingClass()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//加载树数据
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.fitting.fitFitting.edit.post(this.form);
					} else {
						res = await this.$API.fitting.fitFitting.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.id = data.id
			this.form.title = data.title
			this.form.code = data.code
			this.form.sort = data.sort
			this.form.remark = data.remark
			this.form.class_id = data.class_id
			this.form.spec = data.spec
			this.form.unit_id = data.unit_id
			this.form.img = data.img
			this.form.system_disable = data.system_disable
		},
        // 单位
        async getUnit(){
            var res = await this.$API.all.getUnit.get({is_page:0});
            this.unitList = res.data;
        },
        // 备件类型
        async getFittingClass(){
            var res = await this.$API.all.getFittingClass.get({is_page:0});
            this.fitList = res.data;
        },
	}
}
</script>

<style></style>
